/**
 * Created by TomSean on 2017/3/10.
 */
var VehicleManager = function($parent) {
    this.$parent=  $parent;
    this.sysUtils = new kendoUtils();
    this.init();
    return this;
}

VehicleManager.prototype = {
    init : function() {
        var fieldArray = this.sysUtils.getBindFieldArray(this.$parent.find(".search-panel"));
        var functionArray = [
            {fName : "search_vehicle", fs : this.findData},
            {fName : "reset_btn", fs : this.resetInfo}

        ];
        this.bindBaseModels = new bindBaseModel(null, fieldArray, functionArray, null);
        this.sysUtils.bindDoucment(this.bindBaseModels, this.$parent, this);
        Utils.createDataDictDropDownList({div:this.$parent.find("#vehicleType"), parentModelKey:BASIS.VEHICLE_TYPE.ROOT}, this);
        var sHeight = (this.$parent.find('.search-panel')[0].clientHeight);
        var allHeight = (this.$parent[0].clientHeight);
        this.$parent.find('#vehicle_data_list').height(allHeight-sHeight-2);
        this.createGrid();
        this.findData();
    },
    /*
     * 重置按钮
     */
    resetInfo:function(e){
        var that = $(e.target).data("that");
        that.sysUtils.setBindFieldNull(that.bindBaseModels.bindFieldArray);
    },
    findData : function(e) {
        var _this = this;
        if (!isEmpty(e)) {
            _this = $(e.target).data("that");
        }
        var params = _this.sysUtils.getBindDocJson(_this.bindBaseModels.bindFieldArray);
        var dataT = {json : JSON.stringify(params)};
        var dataSource = _this.sysUtils.getDataSource({
            url : baseApiPath + "vehicle/select",
            data : dataT,
            dataType : "odata"
        });
        var grid = _this.$parent.find("#vehicle_data_list").data("kendoGrid");
        grid.setDataSource(dataSource);
    },
    deleteData : function(e) {
        var _this =this;
        if (!isEmpty(e)) {
            _this = $(e.target).data("that");
        }
        var ids = "";
        _this.$parent.find("input[type=checkbox]:checked").each(function(inx, itm) {
            ids += $(itm).val() + ",";
        });
        if (ids == "") {
            notification({title : "操作失败", message : "请勾选需要删除的车辆信息"}, "info");
            return;
        }
        notification({title : "请确认", message : "请确认该车辆是否已经关联包期业务，删除后将无法恢复，是否确认删除？"}, 'confirm', {
            callback : function() {
                JsonpAjax(baseApiPath + "vehicle/delete?ids="+ids, "", "json", function(res) {
                    _this.findData(e);
                });
            }
        })
    },
    openNewWindow : function(e) {
        var _this = this;
        if (!isEmpty(e)) {
            _this = $(e.target).data("that");
        }
        _this.selectRowId = undefined;
        _this.editDiv =_this.sysUtils.openWindow(_this.editDiv, {
            title : "新建车辆信息",
            height : "280px",
            width : "680px",
            resizable : false,
            content : "views/parkLotCenter/parkingLotInfo/vehicle_edit.html"
        });
    },
    openEditWindow : function(e) {
        var _this = this;
        _this.isEdit = true;
        if (!isEmpty(e)) {
            _this = $(e.target).data("that");
        }
        if (!_this.selectRowId) {
            notification({title : "操作失败", message : "请选择需要修改的信息"}, "info");
            return;
        }
        _this.openWindow(_this.selectRowId);
    },
    openWindow : function(id) {
        var _this = this;
        JsonpAjax(baseApiPath + "vehicle/update?id="+id, "", "json", function(res) {
            _this.entity = res;
            _this.editDiv =_this.sysUtils.openWindow(_this.editDiv, {
                title : true == _this.isEdit ? "修改车辆信息" : "查看车辆信息",
                height : "280px",
                width : "680px",
                resizable : false,
                content : "views/parkLotCenter/parkingLotInfo/vehicle_edit.html"
            });
        });
    },
    createGrid : function() {
        var _this = this;
        var columns = [
            {
                width : 30,
                headerTemplate : '<input type="checkbox" gridId="vehicle_data_list" onclick="gridCheckAll(this)" />',
                template : '<input type="checkbox" name="vehicle_id" value="${id}" />'
            },
            {
                title : "车牌号",
                field : "vehicleNo"
            },
            {
                title : "所属车主姓名",
                field : "owner.ownerName"
            },
            {
                title : "所属车主编号",
                field : "owner.ownerCode"
            },
            {
                title : "车辆颜色",
                field : "vehicleColor"
            },
            {
                title : "车辆品牌",
                field : "vehicleCategory"
            },
            {
                title : "类型",
                template:"#:Utils.getModelNameByParentKey(BASIS.VEHICLE_TYPE.ROOT, vehicleType)#"
            }
        ];
        this.sysUtils.createGird(this.$parent.find("#vehicle_data_list"),
            {
                selectable : 'single',
                autoSync : true,
                columnMenu : true,
                pageable : {input : true, numeric : false},
                columns : columns,
                change : function(e) {
                    var selected = $.map(this.select(), function(itm) {
                        return $(itm).find("input[type=checkbox]").val();
                    });
                    _this.selectRowId = selected[0];
                },
                dataBound : function() {
                    var $list = _this.$parent.find("#vehicle_data_list");
                    $list.find("tr").unbind("dblclick");
                    $list.find("tr").dblclick(function(e){
                        var grid = $list.data("kendoGrid");
                        var dataItem = grid.dataItem(grid.select());
                        //打开查看窗口
                        _this.isEdit = false;
                        _this.openWindow(dataItem["id"]);
                    });
                }
            });
    }
}

var VehicleInfoEdit = function($parent) {
    this.$parent = $parent;
    this.sysUtils = new kendoUtils();
    this.init();
    return this;
}
VehicleInfoEdit.prototype = {
    init : function() {
        var _this = this;
        this.entity = vehicleManager.entity;
        var fieldArray = this.sysUtils.getBindFieldArray(this.$parent);
        var functionArray = [
            {fName : "save_info", fs : this.saveInfo},
            {fName : "cancel_info", fs : function() { $("#"+vehicleManager.editDiv).data("kendoWindow").close(); }}
        ];
        Utils.createDataDictDropDownList([{div:_this.$parent.find("#vehicleTypeForEdit"), parentModelKey:BASIS.VEHICLE_TYPE.ROOT}], _this);
        this.bindBaseModels = new bindBaseModel(null, fieldArray, functionArray, null);
        this.sysUtils.bindDoucment(this.bindBaseModels, this.$parent, this);
        if (this.entity) {
            this.entity.ownerCode = this.entity.owner.ownerCode;
            this.sysUtils.setBindFieldValue(this.entity);
        }
        var $ownerCodeComplete = _this.$parent.find("#ownerCodeInput").kendoAutoComplete({
            minLength : 1,
            filter: "contains",
            dataTextField : "ownerCode",
            template : "<span class='k-state-default'>#: ownerCode# <small style='font-size:12px;color:grey'>#: ownerName#</small></span>",
            dataSource : {
                type : "odata",
                serverFiltering: true,
                transport : {
                    read : {
                        url : baseApiPath + "owner_info/search"
                    }
                }
            },
            height : 400
        }).data("kendoAutoComplete");
        if (_this.entity && _this.entity.owner) {
            _this.$parent.find("h1").html("当前车辆的车主是：" + _this.entity.owner.ownerName);
            vehicleManager.entity = this.entity = undefined;
        } else {
            _this.$parent.find("h1").html("当前车辆暂无车主信息");
        }
        if (false == vehicleManager.isEdit) {
            this.$parent.find("input[type]").attr("disabled","disabled");
            this.$parent.find("select").attr("disabled","disabled");
            this.$parent.find("textarea").attr("disabled","disabled");
            this.$parent.find('#vehicleTypeForEdit').data('kendoDropDownList').enable(false);
            //this.$parent.find('.save').remove();
            this.$parent.find('.basicInfo-btn').remove();
        }
    },
    saveInfo : function(e) {
        var _this = this;
        if (!isEmpty(e)) {
            _this = $(e.target).data("that");
        }
        var params = _this.sysUtils.getBindDocJson();
        var ownerCode = _this.$parent.find("#ownerCodeInput").val();
        params["owner.ownerCode"] = params["ownerCode"];
        var dataT = {jsonParam : JSON.stringify(params), id:vehicleManager.selectRowId}
        console.log(dataT);
        if (validityForm(_this.$parent)) {
            JsonpAjax(baseApiPath + "vehicle/update/save", dataT, "json", function(res) {
                vehicleManager.findData();
                $("#"+vehicleManager.editDiv).data("kendoWindow").close();
            });
        }
    }
}